﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Globalization;
using System.Web.UI.DataVisualization.Charting;
using System.Text;
using CommunergyLogic;
using Communergy.Repository;
using Communergy.Repository.EntityFramework;

public partial class meter_MeterSummary : System.Web.UI.Page
{
    int meterId;

    communergyEntities db = new communergyEntities();
    ElectricityMeter meter = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        bool requestValid;
        MembershipUser thisUser = Membership.GetUser();

        if (thisUser != null)
        {
            var userId = thisUser.ProviderUserKey;


            if (Page.IsPostBack)
            {
                meterId = userMeterCombo.MeterId;
                meter = db.ElectricityMeter.Where(m => m.me_id == meterId).FirstOrDefault();
            }
            else
            {
                var meterIdString = Request.QueryString["meterId"];
                meter = Helpers.GetMeterIfBelongsToUser(db, meterIdString);
                if (meter == null)
                {
                    var userGuid = new Guid(thisUser.ProviderUserKey.ToString());
                    meter = db.ElectricityMeter.Where(m => m.me_aspUserId == userGuid).FirstOrDefault();
                }
            }


            requestValid = (meter != null);
            if (requestValid)
            {
                meterId = meter.me_id;
                userMeterCombo.MeterId = meterId;

                meter.ElectricityDaily_Manual.Load();
                lblTotalInfo.Text = string.Format("Total electricity usage by year and month for meter '{0}'", meter.me_meterName);
                hypManageMeter.NavigateUrl = string.Format("~/meter/AddNewManual.aspx?meterId={0}", meter.me_meterGuid);
                LoadAnnualComparisons();
              
                
            }
        }
        else
        {
            requestValid = false;
        }
        panelNoAcccess.Visible = !requestValid;
        panelValid.Visible = requestValid;

    }


    void LoadAnnualComparisons()
    {


        var legendName = "YearLegend";



        var years = from e in meter.ElectricityDaily_Manual
                    group e.de_kWhDaily by e.de_dateTime.Year into annual
                    select annual.Key;

        foreach (var year in years)
        {
            var seriesName = year.ToString();
            //meter.ElectricityDaily_Manual.Load();
            var meterReadings = from e in meter.ElectricityDaily_Manual
                                where e.de_dateTime.Year == year
                                group e.de_kWhDaily by e.de_dateTime.Month into monthly
                                select new
                                {
                                    Month = monthly.Key,
                                    TotalKWh = monthly.Sum()
                                };
            Series series = chartCompareYearsByMonth.Series.Add(seriesName);
            series.CustomProperties = "DrawingStyle=Cylinder";
            series.Legend = legendName;
            foreach (var reading in meterReadings)
            {
                double[] convertedReading = { Convert.ToDouble(reading.TotalKWh) };
                var dateUrl = string.Format("{0}-{1}-1", year, reading.Month);
                var dateTooltip = string.Format("{0}-{1}, {2} kWh", MonthNumberToName(reading.Month), year, reading.TotalKWh);
                var url = string.Format("MeterDaily.aspx?meterId={0}&monthStart={1}", meterId, dateUrl);
                var newDataPointItem = new DataPoint { YValues = convertedReading, Url = url, ToolTip = dateTooltip };
                series.Points.Add(newDataPointItem);

            }

            series.ChartArea = "ChartArea1";
            series.ChartType = SeriesChartType.StackedColumn;
            series.BorderWidth = 2;
        }


    }

    string MonthNumberToName(int number)
    {
        DateTime tempDate = new DateTime(1900, number, 1);
        var monthName = tempDate.ToString("MMM");
        return monthName;
    }

    protected void Unnamed1_Click(object sender, EventArgs e)
    {

    }
   
}
